gravity与layout_gravity的区别

gravity与layout_gravity的区别gravity与layout_gravity的区别

大家好,又见面了,我是你们的朋友全栈君。

含义

gravity是相对于元素本身而言的,用于控制布局中控件的对齐方式,如果没有子控件,则表示内容的对齐方式。gravity常用值有top、bottom、left、right、center等,可以使用”|”将多个值连接起来。

layout_gravity是相对于父布局容器而言的,用于设置在父容器中的对齐方式。

例子

在LinearLayout中放置一个TextView,通过配置gravity和layout_gravity观察TextView出现的位置。
xml文件代码:在TextView中添加属性,android:layout_gravity=”center”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"//设置TextView在父控件LinearLayout中居中
        android:text="@string/hello_world" />

</LinearLayout>
在LinearLayout中修改代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">//设置子控件TextView居中
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别=
gravity与layout_gravity的区别


从上面的运行结果就可以看出,android:layout_gravity=”center”是让TextView显示在Layout的中间。
android:gravity=”center”则让LinearLayout中的子控件TextView居中。
再看一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_horizontal"//添加代码的位置
        android:layout_gravity="center
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别


TextView中没有子控件,那么它会对TextView中的内容起作用。字体在TextView中居中显示。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159825.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UltraEdit 配置 shell 语法高亮

    UltraEdit 配置 shell 语法高亮从UE官网下载wordfile扩展文件包,http://www.ultraedit.com/files/wf/wf.zip,解压后找到文件“unixshell.uew”,将其复制到ue的wordfile配置文件夹.点击菜单上的“高级”–>“配置”勾选“启用语法彩色显示”–>“已安装的词语文”选择bash–>点击应用,重启UE即可本文参考:http://blog.cs

    2022年7月15日
    16
  • ExtJs教程 3.0[通俗易懂]

    ExtJs教程 3.0[通俗易懂]ExtJs3.0入门教程教程

    2022年6月18日
    22
  • 百度云学习资源整合

    百度云学习资源整合关注微信公众号《IT技术资源共享》获取更多资源!微信号:feng1376311650或者扫二维码关注获取下面全部资源:python:【0001】Python安装包,教程文档,工具包链接:http://pan.baidu.com/s/1hsnHizM密码:9×03【0002】Python基础到高级视频教程链接:http://pan.baidu.com/s/1qYE5nOS密码:v0qfjava:【1101】java项目视频20套链接:…

    2022年5月11日
    105
  • LeetCode: Distinct Subsequences [115]

    LeetCode: Distinct Subsequences [115]

    2022年1月2日
    42
  • 我在博客园的新生活

    我在博客园的新生活

    2022年2月22日
    41
  • 香农编码的matlab实现实验总结_香农编码C语言

    香农编码的matlab实现实验总结_香农编码C语言设计shannon编码方法,实现香农编码并算出编码效率实验三香农编码一、[实验目的]1、理解香农第一定理指出平均码长与信源之间的关系;2、加深理解香农编码具有的重要的理论意义。二、[实验环境]windowsXP,MATLAB7三、[实验原理]香农第一定理:设离散无记忆信源为Ss1P=p(s1)熵为H(S),其N次扩展信源为s2…..sqp(s2)….p(sq)SNα1=…

    2025年10月19日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号